package com.atguigu.auth.controller;

import com.atguigu.auth.service.SysRoleService;
import com.atguigu.common.result.Result;
import com.atguigu.model.system.SysRole;
import com.atguigu.vo.system.SysRoleQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author Jason Gong
 * @version 1.0
 * @Date 2025/2/23
 * @Website https://qingling.icu
 * @Description 角色管理
 */
@Api(tags = "角色管理的接口")
@RestController
@RequestMapping("/admin/system/sysRole")
public class SysRoleController {

    @Autowired
    private SysRoleService sysRoleService;

    /**
     * 查询所有角色
     */
    @ApiOperation("查询所有角色")
    @GetMapping("/findAll")
    public Result<List<SysRole>> findAll() {
        return Result.ok(sysRoleService.list());
    }

    /**
     * 条件分页查询
     *
     * @param page  当前页
     * @param limit 每页显示记录数
     * @return 分页结果数据
     */
    @ApiOperation("条件分页查询")
    @GetMapping("/{page}/{limit}")
    public Result<Page<SysRole>> pageQueryRole(@PathVariable Long page, @PathVariable Long limit, SysRoleQueryVo sysRoleQueryVo) {
        Page<SysRole> pageParam = new Page<>(page, limit);
        LambdaQueryWrapper<SysRole> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotEmpty(sysRoleQueryVo.getRoleName())) {
            queryWrapper.like(SysRole::getRoleName, sysRoleQueryVo.getRoleName());
        }
        return Result.ok(sysRoleService.page(pageParam, queryWrapper));
    }

    @ApiOperation(value = "根据id查询角色")
    @GetMapping("get/{id}")
    public Result<SysRole> get(@PathVariable Long id) {
        return Result.ok(sysRoleService.getById(id));
    }

    @ApiOperation(value = "新增角色")
    @PostMapping("/save")
    public Result<Boolean> save(@RequestBody @Validated SysRole role) {
        return sysRoleService.save(role) ? Result.ok() : Result.fail();
    }

    @ApiOperation(value = "修改角色")
    @PutMapping("/update")
    public Result<Boolean> updateById(@RequestBody SysRole role) {
        return sysRoleService.updateById(role) ? Result.ok() : Result.fail();
    }

    @ApiOperation(value = "删除角色")
    @DeleteMapping("/remove/{id}")
    public Result<Boolean> remove(@PathVariable Long id) {
        return sysRoleService.removeById(id) ? Result.ok() : Result.fail();
    }

    @ApiOperation(value = "批量删除角色")
    @DeleteMapping("/batchRemove")
    public Result<Boolean> batchRemove(@RequestBody List<Long> idList) {
        return sysRoleService.removeByIds(idList) ? Result.ok() : Result.fail();
    }
}